package com.etc.controller;

import java.io.IOException;
import java.util.List;

import javax.servlet.ServletException;
import javax.servlet.annotation.WebServlet;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import com.etc.dto.Result;
import com.etc.entity.Collection;
import com.etc.entity.Games;
import com.etc.service.impl.CollectionServiceImpl;
import com.etc.service.impl.GamesServiceImpl;
import com.fasterxml.jackson.databind.ObjectMapper;

/**
 * Servlet implementation class CollectionServlet
 */
@WebServlet("/CollectionServlet")
public class CollectionServlet extends HttpServlet {
	private static final long serialVersionUID = 1L;
       
    /**
     * @see HttpServlet#HttpServlet()
     */
    public CollectionServlet() {
        super();
        // TODO Auto-generated constructor stub
    }

	/**
	 * @see HttpServlet#service(HttpServletRequest request, HttpServletResponse response)
	 */
	protected void service(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
		request.setCharacterEncoding("UTF-8");
		String op = request.getParameter("op");
		ObjectMapper objectMapper = new ObjectMapper();
		String json = "";
		Result result = new Result();
		if("add".equals(op)) {
			String customerId = request.getParameter("customerId");
			String gamesId = request.getParameter("gamesId");
			boolean addCollection = new CollectionServiceImpl().addCollection(Integer.valueOf(customerId), Integer.valueOf(gamesId));
			if(addCollection) {
				result.ok(addCollection);
			}else {
				result.fail(0, "查无数据");
			}
			json = objectMapper.writeValueAsString(result);
		}else if ("find".equals(op)) {
			String customerId = request.getParameter("customerId");
			String gamesId = request.getParameter("gamesId");
			Collection findCollectionByCustomerIdAndGamesId = new CollectionServiceImpl().findCollectionByCustomerIdAndGamesId(Integer.valueOf(customerId), Integer.valueOf(gamesId));
			if(findCollectionByCustomerIdAndGamesId!=null) {
				result.ok(findCollectionByCustomerIdAndGamesId);
			}else {
				result.fail(0, "查无数据");
			}
			json = objectMapper.writeValueAsString(result);
			
			
		}else if ("delete".equals(op)) {
			String customerId = request.getParameter("customerId");
			String gamesId = request.getParameter("gamesId");
			boolean deleteCollectionByCustomerIdAndGamesId = new CollectionServiceImpl().deleteCollectionByCustomerIdAndGamesId(Integer.valueOf(customerId), Integer.valueOf(gamesId));
			if(deleteCollectionByCustomerIdAndGamesId) {
				result.ok(deleteCollectionByCustomerIdAndGamesId);
			}else {
				result.fail(0, "查无数据");
			}
			json = objectMapper.writeValueAsString(result);
			
			
			
		}
		
		
		
		response.setCharacterEncoding("UTF-8");
		response.setContentType("application/json;charset=utf-8");
		response.getWriter().write(json);
		
		
	}

}
